package com.vhuomai.wechat.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.system.constant.UserRole;
import com.vhuomai.wechat.entity.BaseWxTemplate;
import com.vhuomai.wechat.service.WxTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harris
 */
@Api(tags = "微信模板消息")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class WxTemplateController {

    @Autowired
    private WxTemplateService templateService;

    @ApiOperation(value = "模板列表")
    @GetMapping(value = "/wechat/templates", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseWxTemplate>> templates() {
        return ServiceResult.success(templateService.findAll());
    }

    @ApiOperation(value = "添加模板")
    @PostMapping(value = "/wechat/template", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseWxTemplate> addTemplate(@RequestBody BaseWxTemplate template) {
        return ServiceResult.success(templateService.add(template));
    }

    @ApiOperation(value = "修改模板")
    @PutMapping(value = "/wechat/template", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateReply(@RequestBody BaseWxTemplate template) {
        templateService.update(template);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除模板")
    @DeleteMapping(value = "/wechat/template", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> removeReply(@RequestParam("id") int id) {
        templateService.remove(id);
        return ServiceResult.SUCCESS;
    }
}
